import { CloudDbZoneWrapper } from './CloudDBZoneWrapper';

let myHandler = async function (event, context, callback, logger) {
  logger.info(event);
  let result;
  let operation=event.body?JSON.parse(event.body).operation:event.operation;
  let records=event.body?JSON.parse(event.body).records:event.records;
  let cloudDBZoneWrapper = new CloudDbZoneWrapper();
  switch (operation) {
    case "query":
      result = await cloudDBZoneWrapper.queryBooks();
      break;
    case "upsert":
      logger.info(records);
      result = await cloudDBZoneWrapper.upsertBooks(records);
      break;
    case "delete":
      logger.info(records);
      result = await cloudDBZoneWrapper.deleteBooks(records);
      break;
    default:
      return callback({
        ret: {
          code: -1,
          desc: "no such operation"
        }
      });
  }
  logger.info('result: ' + JSON.stringify(result))
  return callback({
    ret: { code: 0, desc: "SUCCESS" },
    result
  });
};

export { myHandler };